package com.itheima.config;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class MybatisConfig {

    @Bean//这个方法是由Spring调用,如果方法有参数，那么Spring会在容器中找对应的值/对象,参数按照类型匹配,相当于省略了@Autowired
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        //1 创建SqlSessionFactoryBean对象
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置参数
        factoryBean.setDataSource(dataSource);
        //factoryBean.setTypeAliasesPackage("com.itheima.bean");//配置别名
        //返回sqlSessionFactoryBean
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        //创建对象
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //设置参数
        msc.setBasePackage("com.itheima.mapper");
        return msc;
    }

}
